package com.sjk.eurekacar.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sjk.eurekacar.entity.TCar;
import com.sjk.eurekacar.entity.TPerson;
import com.sjk.eurekacar.es.EsDao;
import com.sjk.eurekacar.service.ITCarService;
import com.sjk.eurekacar.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sjk
 * @since 2021-11-05
 */
@RestController
@Slf4j
@Transactional
@RequestMapping("/t-car")
public class TCarController {

    @Autowired
    PersonService personService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ITCarService itCarService;
    @Autowired
    KafkaTemplate kafkaTemplate;

    @Autowired
    EsDao esDao;
    /**
     * 登录
     * @param tPerson
     * @return
     */
    @RequestMapping("login")
    public String login(@RequestBody TPerson tPerson){
        TPerson login = personService.login(tPerson);
        System.err.println(login);
        if (!login.getName().equals("false")){
            if (login.getPassword().equals(tPerson.getPassword())){
                return "true";
            }
            return "password";
        }
        return "name";
    }

    /**
     * 列表
     * @param tCar
     */
    @RequestMapping("list")
    public List<TCar> list(@RequestBody TCar tCar){
        if (!redisTemplate.opsForValue().setIfAbsent("person",null,3, TimeUnit.HOURS)){
            log.info("从redis中获取");
            List<TPerson> person = (List<TPerson>) redisTemplate.opsForValue().get("person");
            List<TCar> list1 = itCarService.list();
            for (TCar car:list1) {
                for (TPerson person1:person) {
                    if (car.getUid().equals(person1.getId())){
                        car.setUname(person1.getName());
                    }
                }
            }
            return list1;
        }
        List<TCar> list2 = itCarService.list();
        List<TPerson> list = personService.list();
        System.err.println(list);
        redisTemplate.opsForValue().set("person",list,3, TimeUnit.HOURS);
        for (TCar car:list2) {
            for (TPerson person1:list) {
                if (car.getUid().equals(person1.getId())){
                    car.setUname(person1.getName());
                }
            }
        }
        return list2;
    }


//    @RequestMapping("test")
//    public void test(){
//        redisTemplate.opsForValue().decrement("person");
//    }

    /**
     * 添加
     * @param tCar
     */
    @Transactional
    @RequestMapping("tj")
    public boolean tj(@RequestBody TCar tCar){




        TPerson tPerson = itCarService.getname(tCar.getUname());
        tCar.setUid(tPerson.getId());
        //kafkaTemplate.send("123456", JSON.toJSONString(tCar));

        boolean save = itCarService.save(tCar);
        QueryWrapper<TCar> wrapper = new QueryWrapper<>();
        wrapper.eq("paihao",tCar.getPaihao());
        List<TCar> list = itCarService.list(wrapper);
        for (TCar car:list) {
            esDao.save(car);
        }

        return save;
    }

    @RequestMapping("ps")
    public void ps(@RequestBody TCar tCar){
        Integer[] ids = tCar.getIds();
        for (Integer id:ids) {
            itCarService.removeById(id);
            esDao.deleteById(id);
        }
    }

    @RequestMapping("test")
    public void test(){
        List<TCar> list = itCarService.list();
        for (TCar car:list) {
            esDao.save(car);
        }
    }
}
